package com.yunduansing.core.aop;

import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.yunduansing.core.model.LogModel;
import com.yunduansing.core.model.SysUser;
import com.yunduansing.core.service.LogService;
import com.yunduansing.core.utils.CommonUtil;
import com.yunduansing.core.utils.ResultUtil;

@Aspect
@Component
public class LogAdvices {
	private Logger log;
	private LogModel mLog;
	@Autowired
	private LogService service;
	
	@Around("execution(* com.yunduansing.core.service.impl.*.save*(..))&&!execution(* com.yunduansing.core.service.impl.LogServiceImpl.save*(..))")
	public Object savePointcut(ProceedingJoinPoint jp) throws Throwable{
		log=LoggerFactory.getLogger(jp.getTarget().getClass());
		mLog=new LogModel();
		mLog.setBeginTime(System.currentTimeMillis());
		mLog.setClassName(jp.getTarget().getClass().getName());
		HttpServletRequest req=CommonUtil.getHttpServletRequest();
		SysUser user=(SysUser)(req.getSession().getAttribute("CurrentUser"));
		mLog.setUser(user.getAccount());
		mLog.setUrl(req.getRequestURI());
		mLog.setIp(CommonUtil.getIp(req));
		mLog.setMethod(jp.getSignature().getName());
		mLog.setMsg("保存");
		Object ret=jp.proceed();
		ResultUtil reu=(ResultUtil)ret;
		//log4j日志保存
		if(reu.isFlag()){
			log.info(mLog.getClassName());
			log.info(mLog.getMethod());
			log.info(mLog.getUrl());
			log.info(mLog.getUser());
		}else{
			log.error(mLog.getClassName());
			log.error(mLog.getMethod());
			log.error(mLog.getUrl());
			log.error(mLog.getUser());
		}
		mLog.setEndTime(System.currentTimeMillis());
		mLog.setTime(mLog.getEndTime()-mLog.getBeginTime());
		service.save(mLog);
		return ret;
	}
	
	@Around("execution(* com.yunduansing.core.service.impl.*.delete*(..))&&!execution(* com.yunduansing.core.service.impl.LogServiceImpl.delete*(..))")
	public Object deletePointcut(ProceedingJoinPoint jp) throws Throwable{
		log=LoggerFactory.getLogger(jp.getTarget().getClass());
		mLog=new LogModel();
		mLog.setBeginTime(System.currentTimeMillis());
		mLog.setClassName(jp.getTarget().getClass().getName());
		HttpServletRequest req=CommonUtil.getHttpServletRequest();
		SysUser user=(SysUser)(req.getSession().getAttribute("CurrentUser"));
		mLog.setUser(user.getAccount());
		mLog.setUrl(req.getRequestURI());
		mLog.setIp(CommonUtil.getIp(req));
		mLog.setMethod(jp.getSignature().getName());
		mLog.setMsg("删除");
		Object ret=jp.proceed();
		ResultUtil reu=(ResultUtil)ret;
		//log4j日志保存
		if(reu.isFlag()){
			log.info(mLog.getClassName());
			log.info(mLog.getMethod());
			log.info(mLog.getUrl());
			log.info(mLog.getUser());
		}else{
			log.error(mLog.getClassName());
			log.error(mLog.getMethod());
			log.error(mLog.getUrl());
			log.error(mLog.getUser());
		}
		mLog.setEndTime(System.currentTimeMillis());
		mLog.setTime(mLog.getEndTime()-mLog.getBeginTime());
		service.save(mLog);
		return ret;
	}
	
	@Around("execution(* com.yunduansing.core.service.impl.AccountServiceImpl.login(..))")
	public Object loginPointcut(ProceedingJoinPoint jp) throws Throwable{
		log=LoggerFactory.getLogger(jp.getTarget().getClass());
		mLog=new LogModel();
		mLog.setBeginTime(System.currentTimeMillis());
		mLog.setClassName(jp.getTarget().getClass().getName());
		HttpServletRequest req=CommonUtil.getHttpServletRequest();
		String account=(String) jp.getArgs()[0];
		mLog.setUser(account);
		mLog.setUrl(req.getRequestURI());
		mLog.setIp(CommonUtil.getIp(req));
		mLog.setMethod(jp.getSignature().getName());
		Object ret=jp.proceed();
		ResultUtil reu=(ResultUtil)ret;
		//log4j日志保存
		if(reu.isFlag()){
			mLog.setMsg("登录成功");
			log.info(mLog.getClassName());
			log.info(mLog.getMethod());
			log.info(mLog.getUrl());
			log.info(mLog.getUser());
		}else{
			log.error(mLog.getClassName());
			log.error(mLog.getMethod());
			log.error(mLog.getUrl());
			log.error(mLog.getUser());
			mLog.setMsg("登录失败："+reu.getMsg());
		}
		mLog.setEndTime(System.currentTimeMillis());
		mLog.setTime(mLog.getEndTime()-mLog.getBeginTime());
		if(!mLog.getClassName().contains("LogService"))
			service.save(mLog);
		return ret;
	}
}
